from json import dumps
from src.json_patch import JSONPatch, StatusPatch


class ResultBody:
    """
    ## 通用消息内容
    - report_id: 报告 id
    - data: 结果数据
    - status_patch: 任务状态补丁
    - app_id: 实验标识, 用于让消费者区分来自哪个实验
    - user_id rabbitMQ 的 user_id, 非 学/工号
    """

    def __init__(
        self,
        app_id: str,
        report_id: int,
        data: JSONPatch,
        status_patch: StatusPatch,
    ) -> None:
        super().__init__()
        self.app_id = app_id
        self.report_id = report_id
        self.data = data
        self.status_patch = status_patch.status_patch

    def set_user_id(self, user_id: str):
        self.user_id = user_id

    def set_app_id(self, app_id: str):
        self.app_id = app_id

    def to_json(self):
        return dumps(
            {
                "id": self.report_id,
                "data": self.data.stringify(),
                "status": self.status_patch,
            }
        )

    def to_bytes(self):
        return self.to_json().encode()
